// FontView.cpp : implementation file
//

#include "stdafx.h"
#include "U7FileTool.h"
#include "FontDoc.h"
#include "FontView.h"

#include "Shape.h"

// CFontView

IMPLEMENT_DYNCREATE(CFontView, CScrollView)

CFontView::CFontView()
{
	m_pGraphic = NULL;
}

CFontView::~CFontView()
{
	if ( m_pGraphic )
		DeleteGraphic(m_pGraphic);
}


BEGIN_MESSAGE_MAP(CFontView, CScrollView)
END_MESSAGE_MAP()


// CFontView drawing

void CFontView::OnInitialUpdate()
{
	CScrollView::OnInitialUpdate();

	CSize sizeTotal;
	// TODO: calculate the total size of this view
	CFontDoc* pDoc = (CFontDoc*)GetDocument();
	CSize docSize = pDoc->GetDocSize();

	sizeTotal.cx = docSize.cx;
	sizeTotal.cy = docSize.cy;
	SetScrollSizes(MM_TEXT, sizeTotal);

	m_pGraphic = CreateGraphic( docSize.cx, docSize.cy, 8, ((const BITMAPINFO *)theApp.m_bitmapInfo)->bmiColors );

	PBYTE pFontFile = pDoc->GetFontFile();
	int y = 0;
	for ( int s = 0; s < 10; s++ )
	{
		long shapeLength;
		PSHAPEENTRY shapeEntry = GetShape( (const char*)pFontFile, s, shapeLength );
		int nFrameCount = GetShapeFrameCount( shapeEntry, shapeLength );
		int x = 0;
		int h = 0;
		for ( int i = 0; i < nFrameCount; i++ )
		{
			PFRAMEENTRY frameEntry = GetShapeFrame( shapeEntry, shapeLength, i );
			POINT pt;
			SIZE sz;
			GetShapeFrameInfo( frameEntry, pt, sz );
			DrawShapeFrame( m_pGraphic, frameEntry, x + pt.x, y + pt.y );
			//TRACE("%d,%3d: (%d, %d), (%d,%d)\n", s, i, pt.x, pt.y, sz.cx, sz.cy);
			x += sz.cx;
			if ( sz.cy > h )
				h = sz.cy;
		}
		y += h;
	}
}

void CFontView::OnDraw(CDC* pDC)
{
	CFontDoc* pDoc = (CFontDoc*)GetDocument();
	// TODO: add draw code here
	if ( m_pGraphic )
	{
		::StretchDIBits(
			pDC->GetSafeHdc(),
			0, 0, m_pGraphic->width, m_pGraphic->height,
			0, 0, m_pGraphic->width, m_pGraphic->height,
			m_pGraphic->pBits,
			&m_pGraphic->bmInfo,
			DIB_RGB_COLORS,
			SRCCOPY );
	}
}


// CFontView diagnostics

#ifdef _DEBUG
void CFontView::AssertValid() const
{
	CScrollView::AssertValid();
}

#ifndef _WIN32_WCE
void CFontView::Dump(CDumpContext& dc) const
{
	CScrollView::Dump(dc);
}
#endif
#endif //_DEBUG


// CFontView message handlers
